package com.zyh.controller;

import com.zyh.entity.Category;
import com.zyh.service.CategoryService;
import com.zyh.service.TeaService;
import com.zyh.service.impl.CategoryServiceImpl;
import com.zyh.service.impl.TeaServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {
    ModelAndView mav = null;
    CategoryService categoryService = null;
    @RequestMapping("/selectAllMvc")
    public ModelAndView selectAllMvc(){
        mav = new ModelAndView();
        categoryService = new CategoryServiceImpl();
        List<Category> categoryList = categoryService.selectAll();
        mav.addObject("categoryList",categoryList);
        mav.setViewName("/addTea.jsp");
        return mav;
    }

    @RequestMapping("/selectAllCategoryMvc")
    public ModelAndView selectAllCategoryMvc(){
        mav = new ModelAndView();
        categoryService = new CategoryServiceImpl();
        List<Category> categoryList = categoryService.selectAll();
        mav.addObject("categoryList",categoryList);
        mav.setViewName("/categorylist.jsp");
        return mav;
    }

    @RequestMapping("/deleteOneMvc")
    public ModelAndView deleteOneMvc(Integer categoryIdMy){
        mav = new ModelAndView();
        categoryService = new CategoryServiceImpl();
        TeaService teaService = new TeaServiceImpl();
        teaService.deleteTeasByCategoryId(categoryIdMy);
        categoryService.deleteOne(categoryIdMy);
        mav.setViewName("redirect:/category/selectAllCategoryMvc.do");
        return mav;
    }
}
